<?php

class Main extends CI_Controller {
	public function index()
	{
 	    $response=$_POST["jsonString"];
		$json_decoded = json_decode($response);
		$username = $json_decoded->{'username'};
		$password = $json_decoded->{'password'};
		$tag= $json_decoded->{'tag'};
		$this->load->model('users_model');
        if (isset($tag) && ($tag != ''))
		{
			if ($tag == 'register')
                {
					if ($this->users_model->check_duplicates("$email"))
	               	{
	               		$responseJson = array('tag' => 'register', 'success' => 0, 'error' => 1, 'error_msg' => 'User already existed');
	                    echo json_encode($response);
	                }
	                else
	                {
						$this->users_model->insert_row($name,$email);
						$responseJson = array('tag' => 'register', 'success' => 1, 'error' => 0);
						echo json_encode($response);
	                }
                }
                elseif ($tag == 'login')
                {
					if ($this->users_model->check_if_login_ok($username,$password))
                	{
                        $responseJson = array('tag' => 'login', 'success' => 1, 'error' => 0);
                        echo json_encode($responseJson);
                		die();	
                	}
                }
            }           
        }            
 }
?>                  
                        



